<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" id="text"/>
<input type="button" onclick="send()" value="send" />
<input type="button" onclick="close()" value="close" />
<div id="message"></div>
</body>

<script>
    var webSocket = null;
    if ("webSocket" in window){
        webSocket = new WebSocket("ws:127.0.0.1:8080/websocket/1");
    }else{
        alert("1")
    }
    function setMessageInHtml(message){
        document.getElementById("message").innerHTML+=message;
    }
    webSocket.onopen=function(){
        console.log("发送成功！");
        setMessageInHtml("con sucess");
    }
    function send(){
        var msg = document.getElementById('text').value;
        webSocket.send(msg)
    }
    webSocket.onmessage = function(event){
        setMessageInHtml(event.data)
    }
    function close(){
        webSocket.close(3000,"强制关闭")
    }
    webSocket.onclose = function(){
        setMessageInHtml("colse websocket")
    }
    window.onbeforeunload = function () {
        close();
    }
</script>
</html>
